package com.conversationboard.view;

import java.security.Principal;
import java.sql.SQLException;
import java.util.List;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;

public class MainMenu {

	public static String display(Principal principal) throws SQLException {
		return display(principal, Page.DEFAULT, true);
	}


	public static String display(Principal principal, Page page) throws SQLException {
		return display(principal, page, true);
	}


	public static String display(Principal principal, Page page, boolean showLoginAndRegisterButton) throws SQLException {

		boolean loggedIn = (principal != null);
		User user = null;
		StringBuilder buffer = new StringBuilder();

		if (loggedIn) {
			user = User.get(principal.getName());
		}

		buffer.append("<div class='menubar'>");

		buffer.append("<a title='Display all the most recent posts on the site. You can customise this view in preferences to only show your chosen boards.' class='highlightmenubuttongreen' href='" + Configuration.getInstance().getRoot() + "/SiteNewAnswersControllerServlet'>Site New " + Site.getPost() + "s</a> ");
		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/Search/search.jsp'>");
		buffer.append("Search");
		buffer.append("</a> ");

		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/index.jsp'>Boards</a> ");

		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/PrePreferencesControllerServlet'>Preferences</a> ");

		if (loggedIn) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/BookmarksControllerServlet'>Bookmarks</a> ");
			if (Configuration.getInstance().isUsersCanSeeTheirRecentPosts()) {
				buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/FullTextSearchControllerServlet?terms=" + user.getLoginId() + "&offset=0&searchtype=id&startdate=&enddate=&artefact=messages&idtype=loginid&sortorder=Descending&boardid=0'>My Recent Posts</a> ");
			}
		}

		displayBoardShortcuts(user, buffer, -1);

		if (loggedIn) {
			//buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/Chat/chat.jsp'>Chat</a> ");
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/SiteNewQuestionsControllerServlet'>Site New " + Site.getThread() + "s</a> ");

			if (Configuration.getInstance().isEnableBlogView()) {
				buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/BlogViewControllerServlet?boardid=0'>");
				buffer.append("Blog View");
				buffer.append("</a> ");
			}

			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/confirmlogout.jsp'>Logout</a> ");
		}

		if (page == Page.SEARCH) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/viewthreadsbymonth.jsp" + "'>");
			buffer.append("Browse by Date");
			buffer.append("</a> ");
		}

		if ((!loggedIn) && showLoginAndRegisterButton) {
			buffer.append("<a class='highlightmenubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/register.jsp'>Register</a> ");
			buffer.append("<a class='highlightmenubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/login.jsp'>Login</a> ");
		}

		if (loggedIn && user.isAdmin()) {
			buffer.append("<a href='#' class='highlightmenubutton' onClick='setStyleByClass(\"div\",\"adminlinks\",\"display\", \"inline\");'>");
			buffer.append("Admin");
			buffer.append("</a> ");
		}

		buffer.append("</div>");

		return buffer.toString();
	}


	static void displayBoardShortcuts(User user, StringBuilder buffer, int currentBoardId) throws SQLException {

		/* If you've defined custom boards shortcuts use them */

		if ((user != null) && user.isLoggedIn() && user.getPreferences().isUseBoardShortcuts()) {

			List<Integer> boardShortcuts = user.getPreferences().getBoardShortcuts();

			for (int boardId : boardShortcuts) {
				Board board = Boards.getBoard(boardId);

				if (boardId == currentBoardId) {
					continue;
				}
				
				buffer.append("<a title='" + board.getName() + "' class='highlightmenubuttongreen' href='" + Configuration.getInstance().getRoot() + "/NewAnswersControllerServlet?boardid=" + board.getBoardId() + "'>");
				buffer.append(board.getShortName());
				buffer.append("</a> ");
			}

		} else {

			/* If not, display the popular boards by default, unless it's the current board that you're on */

			for (Board board : Boards.getAuthorisedBoards(user, false)) {

				if (board.isPopularBoard() && (!(board.getBoardId() == currentBoardId))) {
					buffer.append("<a title='" + board.getName() + "' class='highlightmenubuttongreen' href='" + Configuration.getInstance().getRoot() + "/NewAnswersControllerServlet?boardid=" + board.getBoardId() + "'>");
					buffer.append(board.getShortName());
					buffer.append("</a> ");
				}
			}
		}

	}

}
